package com.sunmouse.toolkit.fragment.chart;


import android.graphics.PathEffect;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sunmouse.toollib.utils.ToastUtils;
import com.sunmouse.toolkit.R;
import com.sunmouse.toolkit.fragment.BaseFragment;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import lecho.lib.hellocharts.listener.LineChartOnValueSelectListener;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.ValueShape;
import lecho.lib.hellocharts.view.LineChartView;

/**
 * A simple {@link Fragment} subclass.
 */
public class BrokenFragment extends BaseFragment {


    @BindView(R.id.line_chart)
    LineChartView lineChart;
    Unbinder unbinder;

    private ArrayList<PointValue> points;

    public BrokenFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_broken, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void init() {
        //point mData
        points = new ArrayList<>();
        points.add(new PointValue(0, 2));
        points.add(new PointValue(1, 3));
        points.add(new PointValue(2, 3.5f));
        points.add(new PointValue(3, 3.7f));
        points.add(new PointValue(4, 5));
        points.add(new PointValue(5, 7));
        points.add(new PointValue(6, 5.6f));
        points.add(new PointValue(7, 5.2f));
        points.add(new PointValue(8, 5.0f));


        Line line = new Line(points)
                .setColor(getResources().getColor(R.color.colorAccent))//line color
                .setCubic(true);
        line.setPointColor(getResources().getColor(R.color.colorPrimary));//point color
        line.setStrokeWidth(1);
        line.setSquare(true);

        line.setShape(ValueShape.CIRCLE);//set point shape
//        line.setHasLabels(true);//set label for every point
        line.setHasLines(true);//define whether drawing lines
//        line.setHasPoints(false);//define whether drawing points
        line.setFilled(true);//fill   area
        line.setAreaTransparency(22);//used with setFilled();
        line.setPathEffect(new PathEffect());
        ArrayList<Line> lines = new ArrayList<>();
        lines.add(line);
        LineChartData mData = new LineChartData()
                .setLines(lines);

        Axis xAxis = generateXAxis();
        Axis yAxis = generateYAxis();
        mData.setAxisXBottom(xAxis);
        mData.setAxisYLeft(yAxis);
        this.lineChart.setLineChartData(mData);
        this.lineChart.setOnValueTouchListener(mLineChartOnValueSelectListener);

        /**
         * set view port
         */
//        Viewport v=new Viewport(lineChart.getMaximumViewport());
//        v.left=1;
//        v.top=;
//        v.bottom=0;
//        v.right=points.size()-1;
//        this.lineChart.setMaximumViewport(v);
//        this.lineChart.setCurrentViewport(v);
    }




    /**
     * on point touch listener
     */
    private LineChartOnValueSelectListener mLineChartOnValueSelectListener = new LineChartOnValueSelectListener() {
        @Override
        public void onValueSelected(int i, int i1, PointValue pointValue) {
            ToastUtils.simpleToast(getActivity(), "Point-" + i);
        }

        @Override
        public void onValueDeselected() {

        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
